import 'package:eyepetizer_example/state/base_list_state.dart';
import 'package:flutter/material.dart';

import '../model/common_item.dart';
import '../viewmodel/home_page_viewmodel.dart';
import '../widget/home/banner_widget.dart';
import '../widget/home/list_item_widget.dart';

const TEXT_HEADER_TYPE = 'textHeader';

class HomePageBodyPage extends StatefulWidget {
  const HomePageBodyPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _HomePageBodyState();
}

class _HomePageBodyState extends BaseListState<ItemList,HomePageViewModel, HomePageBodyPage> {
  @override
  Widget getContentChild(HomePageViewModel model) {
    return ListView.separated(itemBuilder: (context, index) {
      if (index == 0) {
        return _banner(model);
      } else {
        if (model.itemList[index].type == TEXT_HEADER_TYPE) {
          return _titleItem(model.itemList[index]);
        }
        return ListItemWidget(item: model.itemList[index]);
      }
    }, separatorBuilder: (context, index) {
      return Padding(
        padding: const EdgeInsets.fromLTRB(15, 0, 15, 0),
        // Divider:分割线
        child: Divider(
          height: model.itemList[index].type == TEXT_HEADER_TYPE || index == 0
              ? 0
              : 0.5,
          color: model.itemList[index].type == TEXT_HEADER_TYPE || index == 0
              ? Colors.transparent
              : const Color(0xffe6e6e6),
        ),
      );
    }, itemCount: model.itemList.length);
  }

  _banner(model) {
    return Container(
      height: 200,
      padding: const EdgeInsets.only(left: 15, top: 15, right: 15),
      // ClipRRect:对子组件进行圆角裁剪
      child: ClipRRect(
        borderRadius: BorderRadius.circular(4),
        child: BannerWidget(model: model),
      ),
    );
  }

  _titleItem(ItemList item) {
    return Container(
      decoration: const BoxDecoration(color: Colors.white24),
      padding: const EdgeInsets.only(top: 15, bottom: 5),
      child: Center(
        child: Text(
          item.data?.text ?? "",
          style: const TextStyle(
            //  bold:粗体
              fontSize: 18,
              color: Colors.black87,
              fontWeight: FontWeight.bold),
        ),
      ),
    );
  }

  @override
  HomePageViewModel get viewModel => HomePageViewModel();
}
